#include "stdio.h"
#include "stdlib.h"

typedef int DataType;
typedef struct StackNode {
    DataType data;
    struct StackNode *next;
} StackNode, *LinkStack;

void InitStack(LinkStack *S) {
    *S = NULL;
}

int StackEmpty(LinkStack S) {
    if(S==NULL)
        return 1;
    else
        return 0;
}

int PushStack(LinkStack *S, DataType x) {
    StackNode *p = malloc(sizeof(StackNode));
    if(!p) {
        return 1;
    }
    p->data = x;
    p->next = *S;
    *S = p;
    return 0;
}

int PopStack(LinkStack *S, DataType *x) {
    StackNode *p ;
    if(StackEmpty(*S))
        return 1;
    *x = (*S)->data;
    p = *S;
    *S = (*S)->next;
    free(p);
    return 0;
}

int GetTopStack(LinkStack S, DataType *x) {
    if(StackEmpty(S))
        return 1;
    *x = S->data;
    return 0;
}

void PrintStack(LinkStack S) {
    int x;
    while(S->next) {
        x = S->next->data;
        printf("%d ", x);
        S = S->next;
    }
    printf("\n");
}
int MAX(StackNode S){
    int max = S.data;
    while(S.next) {
        max = max > S.next->data? max : S.next->data;
        S = *S.next;
    }
    return max;
}

int nums(StackNode S){
    int count = 0;
    while(S.next) {
        count++;
        S = *S.next;
    }
    return count;
}

//求平均数
double avg(StackNode S) {
    int count = nums(S);
    double sum = 0;
    while(S.next) {
        sum += S.next->data;
        S = *S.next;
    }
    return sum/count;
}
//已知f为单链表的表头指针，链表中存储的都是整型数据，试写出实现下列运算的递归算法：
//求链表中的最大整数，
//求链表的结点个数；
//求所有整数的平均值。
int main(){
    StackNode *S;
    InitStack(&S);
    for (int i = 0; i <= 10; i++) {
        PushStack(&S, rand() % 100);
    }
    PrintStack(S);
    printf("%d\n", MAX(*S));
    printf("%d\n", nums(*S));
    printf("%lf\n", avg(*S));
}